Conditions | 69 |
Paths | > 20000 |
Total Lines | 329 |
Lines | 0 |
Ratio | 0 % |
Changes | 20 | ||
Bugs | 1 | Features | 3 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like map.js ➔ initialize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*jslint |
||
416 | function initialize(xcenter, xzoom, xmap, xfeatures, xmarkers, xlines, xgeocache) { |
||
417 | 'use strict'; |
||
418 | |||
419 | var center = null; |
||
420 | var atDefaultCenter = false; |
||
|
|||
421 | var zoom = parseInt(xzoom, 10); |
||
422 | var maptype = xmap; |
||
423 | |||
424 | // parse markers |
||
425 | var markerdata = []; |
||
426 | var markercenter = null; |
||
427 | { |
||
428 | var count = 0; |
||
429 | var clat = 0; |
||
430 | var clon = 0; |
||
431 | |||
432 | // ID:COODS:R(:NAME)?|ID:COORDS:R(:NAME)? |
||
433 | // COORDS=LAT:LON or DEG or DMMM |
||
434 | var data; |
||
435 | if (xmarkers.indexOf("*") != -1) { |
||
436 | data = xmarkers.split('*'); |
||
437 | } else { |
||
438 | /*if (xmarkers.indexOf("|") != -1)*/ |
||
439 | data = xmarkers.split('|'); |
||
440 | } |
||
441 | |||
442 | for (var i = 0; i != data.length; i += 1) { |
||
443 | var data2 = data[i].split(':'); |
||
444 | if (data2.length < 3 || data2.length > 5) continue; |
||
445 | |||
446 | var m = new Object(); |
||
447 | m.alpha = data2[0]; |
||
448 | m.id = alpha2id(m.alpha); |
||
449 | if (m.id == -1) continue; |
||
450 | m.name = null; |
||
451 | |||
452 | var index = 1; |
||
453 | var lat = parseFloat(data2[index]); |
||
454 | var lon = parseFloat(data2[index+1]); |
||
455 | if (lat != null && lon != null && -90 <= lat && lat <= 90 && -180 <= lon && lon <= 180) { |
||
456 | index = index + 2; |
||
457 | m.coords = new google.maps.LatLng(lat, lon); |
||
458 | } else { |
||
459 | m.coords = Coordinates.fromString(data2[index]); |
||
460 | if (m.coords == null) continue; |
||
461 | index = index + 1; |
||
462 | } |
||
463 | |||
464 | var circle = parseFloat(data2[index]); |
||
465 | if (circle < 0 || circle > 100000000000) continue; |
||
466 | m.r = circle; |
||
467 | index = index + 1; |
||
468 | |||
469 | if (index < data2.length) { |
||
470 | if (/^([a-zA-Z0-9-_]*)$/.test(data2[index])) { |
||
471 | m.name = data2[index]; |
||
472 | } |
||
473 | } |
||
474 | |||
475 | count += 1; |
||
476 | clat += m.coords.lat(); |
||
477 | clon += m.coords.lng(); |
||
478 | |||
479 | markerdata.push(m); |
||
480 | } |
||
481 | |||
482 | if (count != 0) { |
||
483 | markercenter = new google.maps.LatLng(clat / count, clon / count); |
||
484 | } |
||
485 | } |
||
486 | |||
487 | var loadfromcookies = false; |
||
488 | if (xcenter != null && xcenter != '') { |
||
489 | var data = xcenter.split(':'); |
||
490 | |||
491 | if (data.length == 1) { |
||
492 | center = Coordinates.fromString(xcenter); |
||
493 | } else { |
||
494 | var lat = parseFloat(data[0]); |
||
495 | var lon = parseFloat(data[1]); |
||
496 | |||
497 | if (lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180) { |
||
498 | center = new google.maps.LatLng(lat, lon); |
||
499 | } |
||
500 | } |
||
501 | } else if (markercenter != null) { |
||
502 | center = markercenter; |
||
503 | } else { |
||
504 | loadfromcookies = true; |
||
505 | |||
506 | /* try to read coordinats from cookie */ |
||
507 | clat = get_cookie_float('clat', CLAT_DEFAULT); |
||
508 | clon = get_cookie_float('clon', CLON_DEFAULT); |
||
509 | if (clat == CLAT_DEFAULT && clon == CLON_DEFAULT) { |
||
510 | atDefaultCenter = true; |
||
511 | } |
||
512 | |||
513 | clat = repairLat(clat, CLAT_DEFAULT); |
||
514 | clon = repairLon(clon, CLON_DEFAULT); |
||
515 | center = new google.maps.LatLng(clat, clon); |
||
516 | |||
517 | zoom = get_cookie_int('zoom', ZOOM_DEFAULT); |
||
518 | maptype = get_cookie_string('maptype', MAPTYPE_DEFAULT); |
||
519 | } |
||
520 | |||
521 | if (center == null) { |
||
522 | center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT); |
||
523 | atDefaultCenter = true; |
||
524 | } |
||
525 | |||
526 | zoom = repairZoom(zoom, ZOOM_DEFAULT); |
||
527 | maptype = repairMaptype(maptype, MAPTYPE_DEFAULT); |
||
528 | |||
529 | var myOptions = { |
||
530 | zoom: zoom, |
||
531 | center: center, |
||
532 | scaleControl: true, |
||
533 | streetViewControl: false, |
||
534 | mapTypeControlOptions: { mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] }, |
||
535 | mapTypeId: google.maps.MapTypeId.ROADMAP }; |
||
536 | |||
537 | map = new google.maps.Map(document.getElementById("themap"), myOptions); |
||
538 | |||
539 | var osm_type = new google.maps.ImageMapType({ |
||
540 | getTileUrl: function(coord, zoom) { |
||
541 | return tileUrl("http://%s.tile.openstreetmap.org/%z/%x/%y.png", ["a","b","c"], coord, zoom); |
||
542 | }, |
||
543 | tileSize: new google.maps.Size(256, 256), |
||
544 | name: "OSM", |
||
545 | alt: "OpenStreetMap", |
||
546 | maxZoom: 18 }); |
||
547 | var osmde_type = new google.maps.ImageMapType({ |
||
548 | getTileUrl: function(coord, zoom) { |
||
549 | return tileUrl("http://%s.tile.openstreetmap.de/tiles/osmde/%z/%x/%y.png", ["a","b","c"], coord, zoom); |
||
550 | }, |
||
551 | tileSize: new google.maps.Size(256, 256), |
||
552 | name: "OSM/DE", |
||
553 | alt: "OpenStreetMap (german style)", |
||
554 | maxZoom: 18 }); |
||
555 | var ocm_type = new google.maps.ImageMapType({ |
||
556 | getTileUrl: function(coord, zoom) { |
||
557 | return tileUrl("http://%s.tile.opencyclemap.org/cycle/%z/%x/%y.png", ["a","b","c"], coord, zoom); |
||
558 | }, |
||
559 | tileSize: new google.maps.Size(256, 256), |
||
560 | name: "OCM", |
||
561 | alt: "OpenCycleMap", |
||
562 | maxZoom: 17 }); |
||
563 | var outdoors_type = new google.maps.ImageMapType({ |
||
564 | getTileUrl: function(coord, zoom) { |
||
565 | return tileUrl("http://%s.tile.thunderforest.com/outdoors/%z/%x/%y.png", ["a","b","c"], coord, zoom); |
||
566 | }, |
||
567 | tileSize: new google.maps.Size(256, 256), |
||
568 | name: "OUTD", |
||
569 | alt: "Thunderforest Outdoors", |
||
570 | maxZoom: 18 }); |
||
571 | var topomap_type = new google.maps.ImageMapType({ |
||
572 | getTileUrl: function(coord, zoom) { |
||
573 | return tileUrl("https://%s.tile.opentopomap.org/%z/%x/%y.png", ["a","b","c"], coord, zoom); |
||
574 | }, |
||
575 | tileSize: new google.maps.Size(256, 256), |
||
576 | name: "TOPO", |
||
577 | alt: "OpenTopoMap", |
||
578 | maxZoom: 15 }); |
||
579 | |||
580 | map.mapTypes.set("OSM", osm_type); |
||
581 | map.mapTypes.set("OSM/DE", osmde_type); |
||
582 | map.mapTypes.set("OCM", ocm_type); |
||
583 | map.mapTypes.set("OUTD", outdoors_type); |
||
584 | map.mapTypes.set("TOPO", topomap_type); |
||
585 | map.setMapTypeId(maptype); |
||
586 | |||
587 | Sidebar.init(map); |
||
588 | ExternalLinks.init(map); |
||
589 | Lines.init(map); |
||
590 | Geolocation.init(map); |
||
591 | Hillshading.init(map); |
||
592 | NPA.init(map); |
||
593 | CDDA.init(map); |
||
594 | Freifunk.init(map); |
||
595 | |||
596 | //boundariesLayer = new google.maps.ImageMapType({ |
||
597 | // getTileUrl: function(coord, zoom) { |
||
598 | // if (6 <= zoom && zoom <= 16) |
||
599 | // { |
||
600 | // return tileUrl("http://korona.geog.uni-heidelberg.de/tiles/adminb/?x=%x&y=%y&z=%z", ["dummy"], coord, zoom); |
||
601 | // } |
||
602 | // else |
||
603 | // { |
||
604 | // return null; |
||
605 | // } |
||
606 | // }, |
||
607 | // tileSize: new google.maps.Size(256, 256), |
||
608 | // name: "adminb", |
||
609 | // alt: "Administrative Boundaries", |
||
610 | // maxZoom: 16 }); |
||
611 | |||
612 | // Create div for showing copyrights. |
||
613 | copyrightDiv = document.createElement("div"); |
||
614 | copyrightDiv.id = "map-copyright"; |
||
615 | copyrightDiv.style.fontSize = "11px"; |
||
616 | copyrightDiv.style.fontFamily = "Arial, sans-serif"; |
||
617 | copyrightDiv.style.margin = "0 2px 2px 0"; |
||
618 | copyrightDiv.style.whiteSpace = "nowrap"; |
||
619 | copyrightDiv.style.background = "#FFFFFF"; |
||
620 | map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(copyrightDiv); |
||
621 | |||
622 | map.setCenter(center, zoom); |
||
623 | |||
624 | google.maps.event.addListener(map, "center_changed", function() { storeZoom(); storeCenter(); okapi_schedule_load_caches(); }); |
||
625 | google.maps.event.addListener(map, "zoom_changed", function() { storeZoom(); storeCenter(); okapi_schedule_load_caches(); }); |
||
626 | google.maps.event.addListener(map, "maptypeid_changed", function(){ updateCopyrights()}); |
||
627 | |||
628 | if (loadfromcookies) { |
||
629 | var raw_ids = Cookies.get('markers'); |
||
630 | if (raw_ids != undefined) { |
||
631 | var ids = raw_ids.split(':'); |
||
632 | for (var i = 0; i != ids.length; ++i) { |
||
633 | var id = parseInt(ids[i], 10); |
||
634 | if (id === null || id < 0 || id >= 26*10) continue; |
||
635 | |||
636 | var raw_data = Cookies.get('marker' + id); |
||
637 | if (raw_data == undefined) continue; |
||
638 | |||
639 | var data = raw_data.split(':') |
||
640 | if (data.length != 3 && data.length != 4) continue; |
||
641 | |||
642 | var lat = parseFloat(data[0]); |
||
643 | if (lat < -90 || lat > 90) continue; |
||
644 | var lon = parseFloat(data[1]); |
||
645 | if (lon < -180 || lon > 180) continue; |
||
646 | var r = parseFloat(data[2]); |
||
647 | if (r < 0 || r > 100000000000) continue; |
||
648 | |||
649 | var name = null; |
||
650 | if (data.length == 4) { |
||
651 | if (/^([a-zA-Z0-9-_]*)$/.test(data[3])) { |
||
652 | name = data[3]; |
||
653 | } |
||
654 | } |
||
655 | |||
656 | newMarker(new google.maps.LatLng(lat, lon), id, r, name); |
||
657 | } |
||
658 | } |
||
659 | |||
660 | var raw_lines = Cookies.get('lines'); |
||
661 | if (raw_lines != undefined) { |
||
662 | var linesarray = raw_lines.split('*'); |
||
663 | for (var i = 0; i < linesarray.length; ++i) { |
||
664 | var line = linesarray[i].split(':'); |
||
665 | if (line.length != 2) continue; |
||
666 | |||
667 | var id1 = alpha2id(line[0]); |
||
668 | if (id1 != -1 && theMarkers.getById(id1).isFree()) { |
||
669 | id1 = -1; |
||
670 | } |
||
671 | var id2 = alpha2id(line[1]); |
||
672 | if (id2 != -1 && theMarkers.getById(id2).isFree()) { |
||
673 | id2 = -1; |
||
674 | } |
||
675 | |||
676 | Lines.newLine(id1, id2); |
||
677 | } |
||
678 | } |
||
679 | } else { |
||
680 | for (var i = 0; i < markerdata.length; ++i) { |
||
681 | newMarker(markerdata[i].coords, markerdata[i].id, markerdata[i].r, markerdata[i].name); |
||
682 | } |
||
683 | |||
684 | var raw_lines = xlines; |
||
685 | if (raw_lines != null) { |
||
686 | /* be backwards compatible */ |
||
687 | if (raw_lines.length == 3 |
||
688 | && raw_lines[0] >= 'A' && raw_lines[0] <= 'Z' |
||
689 | && raw_lines[1] == '*' |
||
690 | && raw_lines[2] >= 'A' && raw_lines[2] <= 'Z') { |
||
691 | raw_lines = raw_lines[0] + ':' + raw_lines[2]; |
||
692 | } |
||
693 | |||
694 | var linesarray = raw_lines.split('*'); |
||
695 | for (var i = 0; i < linesarray.length; ++i) { |
||
696 | var line = linesarray[i].split(':'); |
||
697 | if (line.length != 2) continue; |
||
698 | |||
699 | var id1 = alpha2id(line[0]); |
||
700 | if (id1 != -1 && theMarkers.getById(id1).isFree()) { |
||
701 | id1 = -1; |
||
702 | } |
||
703 | var id2 = alpha2id(line[1]); |
||
704 | if (id2 != -1 && theMarkers.getById(id2).isFree()) { |
||
705 | id2 = -1; |
||
706 | } |
||
707 | |||
708 | Lines.newLine(id1, id2); |
||
709 | } |
||
710 | } |
||
711 | } |
||
712 | |||
713 | okapi_show_cache = xgeocache; |
||
714 | Sidebar.restore(true); |
||
715 | if (xfeatures == '[default]') { |
||
716 | Hillshading.restore(false); |
||
717 | //restoreBoundaries(false); |
||
718 | restoreGeocaches(false); |
||
719 | NPA.toggle(false); |
||
720 | CDDA.toggle(false); |
||
721 | Freifunk.toggle(false); |
||
722 | } else { |
||
723 | Hillshading.toggle(xfeatures.indexOf('h') >= 0 || xfeatures.indexOf('H') >= 0); |
||
724 | //toggleBoundaries(xfeatures.indexOf('b') >= 0 || xfeatures.indexOf('B') >= 0); |
||
725 | okapi_toggle_load_caches(xfeatures.indexOf('g') >= 0 || xfeatures.indexOf('G') >= 0); |
||
726 | NPA.toggle(xfeatures.indexOf('n') >= 0 || xfeatures.indexOf('N') >= 0); |
||
727 | Freifunk.toggle(xfeatures.indexOf('f') >= 0 || xfeatures.indexOf('F') >= 0); |
||
728 | } |
||
729 | restoreCoordinatesFormat(0); |
||
730 | |||
731 | if (xgeocache != "") { |
||
732 | okapi_toggle_load_caches(true); |
||
733 | atDefaultCenter = false; |
||
734 | } |
||
735 | |||
736 | // update copyrights + gmap-stuff now, once the map is fully loaded, and in 1s - just to be sure! |
||
737 | updateCopyrights(); |
||
738 | google.maps.event.addListenerOnce(map, 'idle', function(){ updateCopyrights(); }); |
||
739 | setTimeout(function(){ updateCopyrights(); }, 1000); |
||
740 | |||
741 | //if (atDefaultCenter) { |
||
742 | // Geolocation.whereAmI(); |
||
743 | //} |
||
744 | } |
||
745 |